import { pages, subPackages } from 'virtual:uni-pages';
import { createRouter } from 'uni-mini-router';

// 生成路由配置
function generateRoutes() {
  const routes = pages.map((page) => {
    const newPath = `/${page.path}`;
    // 透传 meta 字段（如果 pages.json 中定义了）
    const meta = page.meta ?? undefined;
    return { ...page, path: newPath, meta };
  });

  // 处理分包路由
  if (subPackages && subPackages.length > 0) {
    subPackages.forEach((subPackage) => {
      const subRoutes = subPackage.pages.map((page) => {
        const newPath = `/${subPackage.root}/${page.path}`;
        const meta = page.meta ?? undefined;
        return { ...page, path: newPath, meta };
      });
      routes.push(...subRoutes);
    });
  }
  return routes;
}

// 创建路由实例
const router = createRouter({
  routes: generateRoutes(),
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.requireAuth) {
    // 先取消本次导航，避免在异步对话框中遗漏 next 导致报错
    next(false);
  } else {
    next();
  }
});

export default router;
